package com.ease.concurrent.thread.communication;

import java.util.concurrent.locks.LockSupport;

/**
 * @author: chenhs
 * @date: Created in 11:21 2019/12/31
 **/
public class Demo2_LockSupport {

    static int i = 0;
    static Thread t1, t2;
    static int limit = 10;

    public static void main(String[] args) {

        t1 = new Thread(() -> {
            while (i < limit) {
                System.out.println("t1:" + (++i));
                /** 唤醒t2*/
                LockSupport.unpark(t2);
                /** 阻塞*/
                LockSupport.park();
            }
        });

        t2 = new Thread(() -> {
            while (i < limit) {
                /** 阻塞*/
                LockSupport.park();
                System.out.println(" t2:" + (++i));
                /** 唤醒t1*/
                LockSupport.unpark(t1);
            }
        });

        t1.start();
        t2.start();
    }
}
